package com.jzo2o.customer.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.CurrentUserInfo;
import com.jzo2o.customer.mapper.ServeProviderMapper;
import com.jzo2o.customer.mapper.WorkerCertificationAuditMapper;
import com.jzo2o.customer.mapper.WorkerCertificationMapper;
import com.jzo2o.customer.model.domain.ServeProvider;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.service.IWorkerCertificationAuditService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;

@Service
public class WorkerCertificationAuditServiceImpl extends ServiceImpl<WorkerCertificationAuditMapper, WorkerCertificationAudit> implements IWorkerCertificationAuditService {
    @Resource
    private WorkerCertificationAuditMapper  workerCertificationAuditMapper;
    @Resource
    private WorkerCertificationMapper  workerCertificationMapper;
    @Resource
    private ServeProviderMapper serveProviderMapper;
    /**
     * 审核服务人员认证信息
     *
     * @param id
     * @param cretificationStatus
     * @param rejectReason
     */
    @Override
    public void auditWorker(Long id, Integer cretificationStatus, String rejectReason) {
        CurrentUserInfo currentUserInfo = UserContext.currentUser();
        String auditName = currentUserInfo.getName();
        Long auditId = currentUserInfo.getId();
        // 根据id查询审核数据
        WorkerCertificationAudit workerCertificationAudit = workerCertificationAuditMapper.selectById(id);
        // 判断是否为空
        if(ObjectUtil.isEmpty(workerCertificationAudit)) {
            throw new ForbiddenOperationException("未查询到数据");
        }
        workerCertificationAudit.setCertificationStatus(cretificationStatus);
        if (StrUtil.isNotBlank(rejectReason)) {
            workerCertificationAudit.setRejectReason(rejectReason);
        }
        workerCertificationAudit.setAuditorId(auditId);
        workerCertificationAudit.setAuditorName(auditName);
        workerCertificationAudit.setAuditStatus(1);
        workerCertificationAudit.setAuditTime(new Date());
        workerCertificationAuditMapper.updateById(workerCertificationAudit);

        // 认证通过后，更新服务人员/机构表中的name字段
        if (cretificationStatus == 2) {
            LambdaUpdateWrapper<ServeProvider> wrapper = new UpdateWrapper<ServeProvider>().lambda()
                    .eq(ServeProvider::getId, workerCertificationAudit.getServeProviderId());
            ServeProvider serveProvider = new ServeProvider();
            serveProvider.setName(workerCertificationAudit.getName());
            serveProviderMapper.update(serveProvider, wrapper);
        }

        // 更新认证信息表
        Long serveProviderId = workerCertificationAudit.getServeProviderId();
        WorkerCertification workerCertification = workerCertificationMapper.selectById(serveProviderId);
        workerCertification.setCertificationStatus(cretificationStatus);
        workerCertificationMapper.updateById(workerCertification);


    }
}
